(define-module (builders pages)
  #:use-module (theme theme)
  #:use-module (haunt site)
  #:use-module (haunt post)
  #:use-module (haunt page)
  #:use-module (haunt html)
  #:use-module (haunt utils)
  #:use-module (utils utils)
  #:declarative? #f
  #:export (pages))

;; my pages builder cannot render pages the same way that my-blog
;; builder does.  my blog builder renders posts, because it gets a
;; list of posts.  However, my pages are NOT posts.

;; Builders are procedures that return one or more page objects (*note
;; Pages::) when applied.  A builder accepts two arguments: A site
;; (*note Sites:: and a list of posts (*note Posts::).

(define (pages)
  (lambda (site posts)
    (flatten
     ;; this loops through all the files in my site's pages directory
     ;; and outputs the appropriate style.
     (let loop ([pages (files-in-dir "./pages")])
       (if (null? pages)
           '()
           (cons
            (let* ([current-sxml-file (car pages)]
                   [current-filename (string-drop-right current-sxml-file 4)]
                   [destination-file (string-append current-filename "html")]
                   [title (string-upcase (string-drop-right current-sxml-file 5) 0 1)])
              (display "distination file is\n")
              (display destination-file)
              (display "\n")
              (make-page destination-file
                         (gnucode-layout site title
                                         (load (string-append "../pages/" current-sxml-file)))
                         sxml->html))
            (loop (cdr pages))))))))
